package com.example.demo.controller;

import com.example.demo.common.domain.*;
import com.example.demo.common.utils.PaymentOrder;
import com.example.demo.mapper.HorderMapper;
import com.example.demo.mapper.OrderListMapper;
import com.example.demo.mapper.ReviewMapper;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OrderController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private HorderExample horderExample;
    @Autowired
    private HorderMapper horderMapper;
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private OrderListMapper orderListMapper;
    @Autowired
    private PaymentOrder paymentOrder;

    @RequestMapping("/order")
    public String ToOrder(ModelMap map, @RequestParam(value = "orderNumber", required = false) String id, @RequestParam(defaultValue = "no") String Msg, Review review, HttpSession session) {
        if (id != null) {
            Horder horder = orderService.searchHorder(id);
            String hotelId = String.valueOf(horder.getHotelId());
            Hotel hotel = orderService.searchHotel(hotelId);
            Double i = Double.parseDouble(hotel.getStarRating());
            hotel.setStarRating(String.valueOf(i * 20));//星级展示以百分比展示
            String data = null;
            if (Msg.equals("yes")) {
                horderMapper.deleteByPrimaryKey(id);
                data = "yes";
            }
            if (Msg.equals("退款")) {
                Horder TuiK = new Horder();
                TuiK.setOrderNumber(id);
                TuiK.setOrderState("申请退款");
                horderMapper.updateByPrimaryKeySelective(TuiK);
            }
            System.out.println(id);
            System.out.println(review.getrData());
            if (review.getDescription() != null && review.getrStar() != null) {
                String Uname = (String) session.getAttribute("LoginUser");
                Integer RStar = review.getrStar() * 20;
                review.setrStar(RStar);
                review.setUname(Uname);
                System.out.println(reviewMapper.insertSelective(review));
            }
            map.put("Review", orderService.searchReview(hotel.getHotelTranslatedName()));
            map.put("data", data);
            map.put("Room", orderService.searchRoom(horder.getRtypeId()));
            map.put("Horder", horder);
            map.put("Hotel", hotel);
        }
        map.put("orderNumber", id);
        map.put("title", "订单详情");
        map.put("orderMsg", "详情");
        return "views/order";
    }


    @RequestMapping("/orderList")
    public String OrderList(ModelMap map, HttpServletRequest httpServletRequest) {
        String Uname = (String) httpServletRequest.getSession().getAttribute("LoginUser");
        System.out.println("adadh:"+Uname);
        map.put("OrderList", orderListMapper.selectByHordrAndHotel(Uname));
        map.put("title", "订单列表");
        map.put("orderMsg", "列表");
        return "views/orderList";
    }

    @RequestMapping("/MakeOrder")
    public String MakeOrder(Model model, Room room, String CheckIntime, String CheckOuttime) {

        System.out.println("刷新一次下订单页面");
        String[] InDate = CheckIntime.split("-");
        String[] OutDate = CheckOuttime.split("-");

        Map map = orderService.selectHAndRName(room, InDate, OutDate);
        model.addAttribute("title", "下单页面");
        model.addAttribute("orderMsg", "创建");
        model.addAttribute("roomTypeName", map.get("roomTypeName"));
        model.addAttribute("hotelName", map.get("hotelName"));
        model.addAttribute("roomPrice", map.get("roomPrice"));
        model.addAttribute("CheckIntime", CheckIntime);
        model.addAttribute("CheckOuttime", CheckOuttime);
        model.addAttribute("dataDiffient", map.get("dataDiffient"));
        model.addAttribute("roomId", room.getRtypeId());
        return "views/MakeOrder";
    }

    @RequestMapping("/toPayment")
    @ResponseBody
    public Map toPayment(Horder horder, HttpSession session) throws Exception {
        String Uname = (String) session.getAttribute("LoginUser");
        System.out.println("插入订单");
        Horder horder1 = orderService.makeOrder(horder, Uname);
        horderMapper.insertSelective(horder1);
        System.out.println("打印：" + horder1);
        String ordernum = horder1.getOrderNumber();
        String orderPrice = horder1.getPrice().toString();
        System.out.println("aaa" + ordernum + ";" + orderPrice);
        Map map = new HashMap();
        map.put("ordernum", ordernum);
        map.put("orderPrice", orderPrice);
        return map;

    }

    @RequestMapping("/toPaymenttransfer")
    public String toPtoPaymenttransferayment(String ordernum, String price,HttpSession session) throws Exception {
        System.out.println("中转：" + ordernum + ";" + price);
        return "redirect:/goAlipay?orderId=" + ordernum + "&account=" + price;
    }

}